package me.sealer.plugins;

import me.sealer.plugins.util.FileCountUtil;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Mojo(name = "count-files-amount")
public class CountFilesAmountMojo extends AbstractMojo {
    private static final String[] INCLUDES_DEFAULT = {"java"};

    @Parameter(defaultValue = "${project.basedir}")
    private File baseDir;

    @Parameter(defaultValue = "${project.build.sourceDirectory}")
    private File sourceDirectory;

    @Parameter(defaultValue = "${project.build.testSourceDirectory}")
    private File testSourceDirectory;

    @Parameter(defaultValue = "${project.build.resources}")
    private List<Resource> resources;

    @Parameter(defaultValue = "${project.build.testResources}")
    private List<Resource> testResources;

    private String artifactId;

    @Parameter
    private String[] includes;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        if (includes == null || includes.length == 0) {
            includes = INCLUDES_DEFAULT;
        }

        List<File> collected = new ArrayList<>();
        try {
            countDir(sourceDirectory, collected);
            countDir(testSourceDirectory, collected);
            for (Resource resource : resources) {
                countDir(new File(resource.getDirectory()), collected);
            }

            for (Resource resource : testResources) {
                countDir(new File(resource.getDirectory()), collected);
            }
        } catch (IOException e) {
            throw new MojoExecutionException("Unable to count files' amount.", e);
        }
        getLog().info(" files amount in project " + (artifactId == null ? "" : artifactId) + " is : " + collected.size());
    }

    /**
     * 统计dir目录下的所有文件的数量
     *
     * @param dir
     * @throws IOException
     */
    private void countDir(File dir, List<File> collected) throws IOException {
        if (!dir.exists()) {
            return;
        }
        FileCountUtil.collectFiles(collected, dir, this.includes);
    }

    public void setBaseDir(File baseDir) {
        this.baseDir = baseDir;
    }

    public void setSourceDirectory(File sourceDirectory) {
        this.sourceDirectory = sourceDirectory;
    }

    public void setTestSourceDirectory(File testSourceDirectory) {
        this.testSourceDirectory = testSourceDirectory;
    }

    public void setResources(List<Resource> resources) {
        this.resources = resources;
    }

    public void setTestResources(List<Resource> testResources) {
        this.testResources = testResources;
    }

    public void setIncludes(String[] includes) {
        this.includes = includes;
    }

    public void setArtifactId(String artifactId) {
        this.artifactId = artifactId;
    }
}
//maven plugin 学习网址

//http://blog.csdn.net/chinrui/article/details/66472815
//http://blog.csdn.net/chinrui/article/details/66472815
//http://maven.apache.org/guides/plugin/guide-java-plugin-development.html